#include "FlashRuntimeExtensions.h"
#import <UIKit/UIKit.h>

//
// C Function Definitions
//

extern "C" void OurExtensionEvent(const char *eventId, const char *level);

//
// Obj-C Delegate Object
//


@interface OurExtensionDelegate : NSObject <UIAlertViewDelegate>

- (void) showMessageBox:(NSString*) message;

@end

@implementation OurExtensionDelegate

/** Show Message Box */
- (void) showMessageBox:(NSString*) message
{
    UIAlertView *alertView=[[UIAlertView alloc]
                            initWithTitle:@"Alert"
                            message: message
                            delegate:self
                            cancelButtonTitle:@"OK"
                            otherButtonTitles: nil];
    
    [alertView show];
    [alertView release];
}

/** UIAlertViewDelgate dismiss callback */
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    OurExtensionEvent("COMPLETE", "");
}

@end


//
// FRE Implementation
//

extern "C"
{

    /** Context Pointer */
    FREContext OurExtensionContext=nil;
        
    /** Reachability Handler */
    OurExtensionDelegate *ourExtensionDelegate=nil;
        
    //
    // FRE Implementation
    //

    /** Show Message Box */
    FREObject showMessageBox(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
    {
        const uint8_t *message;
        uint32_t messageLength;
        FREGetObjectAsUTF8(argv[0], &messageLength, &message);
        
        [ourExtensionDelegate showMessageBox:[NSString stringWithUTF8String:(const char*)message]];
         
        return NULL;
    }
        
      
    /** Content Initializer */
    void OurContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx,
                                            uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
    {
        *numFunctionsToTest=1;
        FRENamedFunction* func=(FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);
                
        func[0].name=(const uint8_t*)"showMessageBox";
        func[0].functionData=NULL;
        func[0].function=&showMessageBox;
                
        OurExtensionContext=ctx;
        *functionsToSet=func;
        
        ourExtensionDelegate=[[OurExtensionDelegate alloc] init];
    }
            
    /** Context Finalizer */
    void OurContextFinalizer(FREContext ctx)
    {
        OurExtensionContext=nil;
    }
        

    /** Extension Initializer */
    void OurExtensionInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet,
                                    FREContextFinalizer* ctxFinalizerToSet)
    {
        *extDataToSet=NULL;
        *ctxInitializerToSet=&OurContextInitializer;
        *ctxFinalizerToSet=&OurContextFinalizer;
    }
        
    /** Extension Finalizer */
    void OurExtensionFinalizer(void* extData)
    {
        return;
    }


    /** Send Event Back to AS3 */
    void OurExtensionEvent(const char *eventId, const char *level)
    {
        if(OurExtensionContext!=nil)
        {
            FREDispatchStatusEventAsync(OurExtensionContext, (uint8_t*)eventId,(uint8_t*)level);
        }
    }
}
